<?php

namespace App\Http\Controllers\V1\Api;

use App\Http\Requests\UserRequest;
use App\Models\V1\Admins;
use App\Models\V1\Advert;
use App\Models\V1\Bank;
use App\Models\V1\BankClass;
use App\Models\V1\DeveloperOperationLog;
use App\Models\V1\Game;
use App\Models\V1\SysPermission;
use App\Models\V1\SysRole;
use App\Models\V1\UpdateLog;
use App\Models\V1\Users;
use Earnp\GoogleAuthenticator\GoogleAuthenticator;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;

class SonUserController extends BaseController
{

    /**
     * 新增用户
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function Add(Request $request)
    {
        try {
            DB::beginTransaction();
            $users_model = new Users();

            if ($users_model->where('mobile', $request->input('mobile'))->exists()) {
                return ajax_output(300, "手机号已经存在");
            }

            $users_model->create([
                'username' => $request->input('username', ""),
                'pid' => $request->input('pid', ""),
                'password' => bcrypt($request->input('password', "")),
                'mobile' => $request->input('mobile', ""),
                'nickname' => $request->input('nickname', ""),
                'state' => 1,
            ]);
            DB::commit();
        } catch (\Exception $e) {
            DB::rollBack();
            return ajax_output(300, "数据异常:" . $e->getMessage());
        }
        // 添加开发者操作日志
        DeveloperOperationLog::query()->create([
            'user' => $request['pid'],
            'ip' => $request->getClientIp(),
            'ulid'=>time(),
            'attributes' => !empty($request->all()) ? json_encode($request->all()) : null,
            'remark' => '添加子账号',
        ]);
        return ajax_output(200, "成功");
    }



    /**
     * 用户列表
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function List(Request $request)
    {

        $query = Users::query();
        $pid = $request->input('pid', "");
        $pageSize = $request->input('pageSize', 10);
        $order = $request->input('order', 'desc');
        $sort = $request->input('sort', 'id');



        if ($request->filled('username')) {
            $query->where('username', $request->input('username'));
        }
        $query->where('state', 1);
        $query->where('pid', $pid);

        $query->orderBy($sort, $order);
        $data = $query->paginate($pageSize);

        // 添加开发者操作日志
        DeveloperOperationLog::query()->create([
            'user' => $request['pid'],
            'ip' => $request->getClientIp(),
            'ulid'=>time(),
            'attributes' => !empty($request->all()) ? json_encode($request->all()) : null,
            'remark' => '查询子账号列表',
        ]);

        return ajax_output(200, "成功", $data);
    }


    /**
     * 更新子账号
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function Update(Request $request)
    {


        $id = $request->input('id', "");
        $users_model = new Users();
        $vo = $users_model->find($id);

        $vo->update([
            'mobile' => $request->input('mobile', ""),
            'nickname' => $request->input('nickname', ""),
        ]);

        // 添加开发者操作日志
        DeveloperOperationLog::query()->create([
            'user' => $vo['pid'],
            'ip' => $request->getClientIp(),
            'ulid'=>time(),
            'attributes' => !empty($request->all()) ? json_encode($request->all()) : null,
            'remark' => '更新子账号',
        ]);

        return ajax_output(200, "成功");
    }


    /**
     * 删除子账号
     *
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function Destroy(Request $request)
    {
        $id = $request->input('id', "");

        $users_model = new Users();
        $vo = $users_model->find($id);

        Users::destroy($id);
        // 添加开发者操作日志
        DeveloperOperationLog::query()->create([
            'user' => $vo['pid'],
            'ip' => $request->getClientIp(),
            'ulid'=>time(),
            'attributes' => !empty($request->all()) ? json_encode($request->all()) : null,
            'remark' => '除子账号',
        ]);
        return ajax_output(200,"成功");
    }















}
